package com.example.myproject.controller;

import com.example.myproject.common.ResponseData;
import com.example.myproject.dto.UpdatePermissionsRequest;
import com.example.myproject.dto.admin.PendingTorrentListResponse;
import com.example.myproject.dto.admin.ReviewRequest;
import com.example.myproject.dto.admin.ReviewResponse;
import com.example.myproject.dto.admin.UserInfoAdminDTO;
import com.example.myproject.service.AdminService;
import com.example.myproject.service.PermissionService;
import com.example.myproject.vo.ApiResponse;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
@RestController
@PreAuthorize("hasRole('ADMIN')")
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private PermissionService permissionService;

    @PutMapping("/user/{userId}/permissions")
    public ApiResponse<Void> updatePermissions(
            @PathVariable Long userId,
            @RequestBody UpdatePermissionsRequest req) {
        permissionService.updatePermissions(userId, req);
        return ApiResponse.success(200, "用户权限更新成功");
    }
    // 添加一个POST测试路由
    @PostMapping("/echo")
    public ApiResponse<String> echoMessage(@RequestBody String message) {
        return ApiResponse.success(200, "回显成功", "你发送的消息是: " + message);
    }
    // 添加一个简单的GET测试路由
    @GetMapping("/test")
    public ApiResponse<String> testAdminRoute() {
        return ApiResponse.success(200, "Admin路由测试成功", "这是一个简单的测试响应");
    }
    @Autowired
    private AdminService adminService;

    @GetMapping("/torrents/pending")
    public ResponseEntity<?> getPendingTorrents(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "page_size", defaultValue = "20") Integer pageSize) {
        PendingTorrentListResponse response = adminService.getPendingTorrents(page, pageSize);
        return ResponseEntity.ok(ResponseData.success(response));
    }

    @PostMapping("/review")
    public ResponseEntity<?> reviewTorrent(@RequestBody ReviewRequest request) {
        log.info("审核资源请求: {}", request);
        try {
            if (request.getTorrentId() == null) {
                return ResponseEntity.ok(ResponseData.fail(404,"资源ID不能为空"));
            }
            if (request.getAction() == null) {
                return ResponseEntity.ok(ResponseData.fail(404,"操作类型不能为空"));
            }
            ReviewResponse response = adminService.reviewTorrent(request);
            return ResponseEntity.ok(ResponseData.success(response));
        } catch (IllegalArgumentException e) {
            log.error("审核资源参数错误", e);
            return ResponseEntity.ok(ResponseData.fail(404,e.getMessage()));
        } catch (Exception e) {
            log.error("审核资源失败", e);
            return ResponseEntity.ok(ResponseData.fail(404,"审核资源失败: " + e.getMessage()));
        }
    }


    @GetMapping("/users")
    public ApiResponse<List<UserInfoAdminDTO>> getAllUsers() {
        List<UserInfoAdminDTO> users = adminService.getAllUsers();
        return ApiResponse.success(200, "获取成功", users);
    }
    @PostMapping("/user/level")
    public ApiResponse<String> updateUserLevel(@RequestBody UpdateUserLevelRequest request) {
        if (request.getUserId() == null || request.getLevel() == null) {
            return ApiResponse.error(400, "userId和level不能为空");
        }
        adminService.updateUserLevel(request.getUserId(), request.getLevel());
        return ApiResponse.success(200, "修改成功", null);
    }

    @Data
    public static class UpdateUserLevelRequest {
        private Long userId;
        private Integer level;
    }

}